<template>
    <el-form ref="form" :model="formData" :label-width="formLabelWidth">

        <el-form-item label="名称/账号：">
            <span class="font-weight-bold">{{dataEntity.name}}  / {{dataEntity.account}}</span>
        </el-form-item>


        <el-form-item label="角色" size="mini">

            <el-checkbox-group v-model="formData.role_ids">
                <el-checkbox
                    v-for="(item,i) in roleOptions"
                    :label="item.value"
                    :key="i"
                    class="d-block"
                >{{item.label}}

                </el-checkbox>
                <br>
            </el-checkbox-group>

        </el-form-item>


        <el-form-item label="">
            <el-button type="primary" @click="submit">保存</el-button>

        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        props: {
            formLabelWidth: {
                type: String,
                default: '120px'
            },
            dataEntity: {
                type: Object,
                default: null
            },

            roleOptions: {
                type: Array,
                default: []
            },
        },

        data() {
            return {
                accesses: [],
                formData: {
                    user_id: '',
                    role_ids: []
                },

            }
        },
        created() {
            let _this = this;
            this.assignData();
        },
        destroyed() {

        },
        mounted() {
            let _this = this

        },
        methods: {
            assignData: function () {
                let _this = this;
                _this.formData.user_id = LeUtils.valueGet(_this.dataEntity, 'id', '');
                let user_roles = LeUtils.valueGet(_this.dataEntity, 'roles', []);
                for (let i in user_roles) {
                    let role_id = LeUtils.valueGet(user_roles[i], 'id', '');
                    if (role_id) {
                        _this.formData.role_ids.push(role_id);
                    }
                }

            },

            submit: function (event) {

                if (event) {
                    event.preventDefault()
                }
                let _this = this;
                let role_id = LeUtils.valueGet(this.dataEntity, 'id');
                LeRoute.request('api.l-e-s.access-control.user.roles.save', _this.formData).then(function (res_data) {
                    _this.$message.success('保存成功')
                })
            },

        }
    }
</script>
